package zooin.net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class AimsHandover {
	private static final String TAG = "AimsHandover";
	
	private DatagramSocket 	sendSocket 	= null;
	private DatagramPacket 	sendPacket 	= null;
	private InetAddress 	aims_ip 		= null;
	private int 			GUI_HOEVENT	= 0X6004;
	private int 			aims_port;
	private byte[] 			sendData;
	
	public AimsHandover(){
		try {
			sendSocket = new DatagramSocket();
			aims_ip = InetAddress.getByName("127.0.0.1");
			aims_port = 7941;
		} catch (SocketException e1) {
		} catch (UnknownHostException e) {
		}
	}
	
	public void sendHOMessage() {

		byte[] bytes = new byte[4];
		bytes[0] = (byte) ((GUI_HOEVENT & 0xFF000000) >> 24);
		bytes[1] = (byte) ((GUI_HOEVENT & 0x00FF0000) >> 16);
		bytes[2] = (byte) ((GUI_HOEVENT & 0x0000FF00) >> 8);
		bytes[3] = (byte) (GUI_HOEVENT & 0x000000FF);

		int newValue = 0;
		newValue |= (((int) bytes[3]) << 24) & 0xFF000000;
		newValue |= (((int) bytes[2]) << 16) & 0xFF0000;
		newValue |= (((int) bytes[1]) << 8) & 0xFF00;
		newValue |= (((int) bytes[0])) & 0xFF;		

		sendData = new byte[32];
		System.arraycopy(intToByte4(newValue), 0, sendData, 0, 4);
		sendPacket = new DatagramPacket(sendData, sendData.length, aims_ip, aims_port);
		try {
			sendSocket.send(sendPacket);
		} catch (IOException e) {
		}
	}
	
	private byte[] intToByte4(int value){
		byte[] arr = new byte[4];
		arr[3] = (byte)(value >> 24);
		arr[2] = (byte)(value >> 16);
		arr[1] = (byte)(value >> 8);
		arr[0] = (byte)value;
		return arr;
	}
}